#include "syscall.h"

void CreateAndDeleteLock()
{
	
	int lock0, lock1, neg1, a, b;
	PrintString("NOW RUNNING TEST [1], CREATE AND DELETE LOCK\n",sizeof("NOW RUNNING TEST [1], CREATE AND DELETE LOCK\n"));
	/*Call delete on a non-existent lock.*/ 
	if(DeleteLock(-2)== 0)
	{
		PrintString("Lock could not be deleted because the lock does not exist\n",
			sizeof("Lock could not be deleted because the lock does not exist\n"));
	}
	else
	{
		PrintString("THIS SHOULD NEVER PRINT. Deleting a non-existent lock test failed.\n",
			sizeof("THIS SHOULD NEVER PRINT. Deleting a non-existent lock test failed.\n"));
	}
	/*Call Create Lock with a bad length*/
	neg1 = CreateLock("testLock", -1);
	if(neg1 == -1)
	{
		PrintString("Lock cannot be created with a bad length\n",sizeof("Lock cannot be created with a bad length\n"));
	}
	/*Create 2 locks.*/
	lock0 = CreateLock("testLock0", 9);
	lock1 = CreateLock("testLock1", 9);
	if(lock0 >= 0 && lock1 >= 0)
	{
		PrintString("2 test locks succesfully created\n",sizeof("2 test locks succesfully created\n"));
	}
	/*Delete them both.*/
	DeleteLock(lock0);
	DeleteLock(lock1);
	PrintString("======TEST 1 HAS COMPLETED======\n",sizeof("======TEST 1 HAS COMPLETED======\n"));
}

void AcquireAndRelease()
{
	
	int lock0, lock1;
	int test0, test1;
	PrintString("NOW RUNNING TEST [2], ACQUIRE AND RELEASE\n",sizeof("NOW RUNNING TEST [2], ACQUIRE AND RELEASE\n"));
	/*Attempt to acquire a lock that does not exist.*/
	test0 = AcquireLock(-42);
	if(test0 == 0)
	{
		PrintString("Cannot acquire a lock that does not exist.\n",
			sizeof("Cannot acquire a lock that does not exist.\n"));
	}
	/*Attempt to release a lock that does not exist.*/
	test1 = ReleaseLock(-42);
	if(test1 == 0)
	{
		PrintString("Cannot release a lock that does not exist.\n",
			sizeof("Cannot release a lock that does not exist.\n"));
	}
	/*make our locks here.*/
	lock0 = CreateLock("testLock0", 9);
	/*acquire the lock here.*/
	if(AcquireLock(lock0))
	{
		PrintString("testLock0 successfully acquired.\n",sizeof("testLock0 successfully acquired.\n"));
	}
	/*release it here.*/
	if(ReleaseLock(lock0))
	{
		PrintString("testLock0 successfully released.\n",sizeof("testLock0 successfully released.\n"));
	}
	/*delete the lock here.*/
	DeleteLock(lock0);
	PrintString("======TEST 2 HAS COMPLETED======\n",sizeof("======TEST 2 HAS COMPLETED======\n"));
}

void CreateAndDeleteCV()
{
	
	int cv0, cv1, neg1;
	PrintString("NOW RUNNING TEST [3], CREATE AND DELETE CV\n",sizeof("NOW RUNNING TEST [3], CREATE AND DELETE CV\n"));
	/*Call delete on a non-existent cv.*/ 
	if(DeleteCondition(-2)== 0)
	{
		PrintString("CV could not be deleted because the CV does not exist\n",
			sizeof("CV could not be deleted because the CV does not exist\n"));
	}
	else
	{
		PrintString("THIS SHOULD NEVER PRINT. Deleting a non-existent cv test failed.\n",
			sizeof("THIS SHOULD NEVER PRINT. Deleting a non-existent cv test failed.\n"));
	}
	/*Call Create cv with a bad length*/
	neg1 = CreateCondition("testCV", -1);
	if(neg1 == -1)
	{
		PrintString("CV cannot be created with a bad length\n",sizeof("CV cannot be created with a bad length\n"));
	}
	/*Create 2 cvs.*/
	cv0 = CreateCondition("testCV0", 7);
	cv1 = CreateCondition("testCV1", 7);
	if(cv0 >= 0 && cv1 >= 0)
	{
		PrintString("2 test cvs succesfully created\n",sizeof("2 test cvs succesfully created\n"));
	}
	/*Delete them both.*/
	DeleteCondition(cv0);
	DeleteCondition(cv1);
	PrintString("======TEST 3 HAS COMPLETED======\n",sizeof("======TEST 3 HAS COMPLETED======\n"));
}

void WaitSignalBroadcast()
{
	
	int lock0, lock1, cv0, cv1, test0, test1, test2;
	PrintString("NOW RUNNING TEST [4], WAIT SIGNAL BROADCAST\n",sizeof("NOW RUNNING TEST [4], WAIT SIGNAL BROADCAST\n"));
	/*attempt to wait on a lock and cv which does not exist*/
	test0 = WaitCondition(-20,-20);
	if(test0 == 0)
	{
		PrintString("Cannot wait on a lock and CV that don't exist.\n",sizeof("Cannot wait on a lock and CV that don't exist.\n"));
	}
	/*attempt to signal on a lock and cv which does not exist*/
	test1 = SignalCondition(-20,-20);
	if(test1 == 0)
	{
		PrintString("Cannot signal on a lock and CV that don't exist.\n",sizeof("Cannot signal on a lock and CV that don't exist.\n"));
	}
	/*attempt to broadcast on a lock and cv which does not exist*/
	test2 = BroadcastCondition(-20,-20);
	if(test2 == 0)
	{
		PrintString("Cannot broadcast on a lock and CV that don't exist.\n",sizeof("Cannot broadcast on a lock and CV that don't exist.\n"));
	}
	lock0 = CreateLock("testLock0", 9);
	cv0 = CreateCondition("testCV0", 7);
	if(SignalCondition(cv0, lock0))
	{
		PrintString("Signal successfully executed.\n",sizeof("Signal successfully executed.\n"));
	}
	if(BroadcastCondition(cv0, lock0))
	{
		PrintString("Broadcast successfully executed.\n", sizeof("Broadcast successfully executed.\n"));
	}
	PrintString("======TEST 4 HAS COMPLETED======\n",sizeof("======TEST 4 HAS COMPLETED======\n"));
}

void PrintTest()
{
	PrintString("NOW RUNNING TEST [5], PRINT TEST\n",sizeof("NOW RUNNING TEST [5], PRINT TEST\n"));
	/*This should print the number 0*/
	Print("Test Printing 0: %d\n", sizeof("Test Printing 0: %d\n"), -1, -1);
	/*This should print the number 1*/
	Print("Test Printing 1: %d\n", sizeof("Test Printing 1: %d\n"), 10000,-1);
	/*This should print the number 0,1*/
	Print("Test Printing 0,1: %d,%d\n", sizeof("Test Printing 0,1: %d,%d\n"),1,-1);
	/*This should print the number 1,0*/
	Print("Test Printing 1,0: %d,%d\n", sizeof("Test Printing 1,0: %d,%d\n"),10000,-1);
	/*This should print the number 1,2,3*/
	Print("Test Printing 1,2,3: %d,%d,%d\n", sizeof("Test Printing 1,2,3: %d,%d,%d\n"),10002,30000);
	/*This should print the number 1,2,3,4*/
	Print("Test Printing 1,2,3,4: %d,%d,%d,%d\n", sizeof("Test Printing 1,2,3,4: %d,%d,%d,%d\n"),10002,30004);
	PrintString("======TEST 5 HAS COMPLETED======\n",sizeof("======TEST 5 HAS COMPLETED======\n"));
}

int main()
{
	PrintString("===========================================\n",sizeof("===========================================\n"));
	PrintString("===WELCOME TO THE LOCK AND CV TESTSUITE====\n",sizeof("===WELCOME TO THE LOCK AND CV TESTSUITE====\n"));
	PrintString("===========================================\n",sizeof("===========================================\n"));
	/*call test 1*/
	CreateAndDeleteLock();	
	/*call test 2*/
	AcquireAndRelease();	
	/*call test 3*/
	CreateAndDeleteCV(); 
	/*call test 4*/
	WaitSignalBroadcast();
	/*call test 5*/
	PrintTest();
	Exit(0);
	return 1;
}
